package com.bit.frige.sub.food;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bit.frige.R;
import com.bit.frige.TabtestActivity;
import com.bit.frige.DB.DBContactHelper;
import com.bit.frige.DB.FoodDTO;

public class SubFoodFormActivity extends Activity {
        private EditText mDateDisplay;
        private EditText mDateDisplay2;
        private DBContactHelper db;
         private int mYear;
         private int mMonth;
         private int mDay;
        private int mYear2; //유통기한
        private int mMonth2;//유통기한
        private int mDay2;//유통기한
        private Button savebtn;
        private Button cancelbtn;
        private int selflife;
        private int cateno;
        private int place;
        
        private String foodname;
         static final int DATE_DIALOG_ID = 0;
         static final int DATE_DIALOG_ID2 = 1;

        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_food_form);
        ImageView img = (ImageView) findViewById(R.id.foodimg);
        Intent intent = getIntent();
        foodname = intent.getStringExtra("foodname");
        selflife = intent.getIntExtra("selflife", 7);
        cateno = intent.getIntExtra("cateno", 0);
        place = intent.getIntExtra("place", 0);
        String catename ="";
        if(cateno==0){
                img.setImageResource(R.drawable.food1);
                catename="육류";
        }else if(cateno==1){
                img.setImageResource(R.drawable.food2);
                catename="생선";
        }else if(cateno==2){
                img.setImageResource(R.drawable.food3);
                catename="해산물";
        }else if(cateno==3){
                img.setImageResource(R.drawable.food4);
                catename="채소";
        }else if(cateno==4){
                img.setImageResource(R.drawable.food5);
                catename="과일";
        }else if(cateno==5){
                img.setImageResource(R.drawable.food6);
                catename="유제품";
        }else if(cateno==6){
                img.setImageResource(R.drawable.food7);
                catename="음료";
        }else if(cateno==7){
                img.setImageResource(R.drawable.food8);
                catename="기타";
        }
        
        TextView foodtext = (TextView) findViewById(R.id.foodname);
        foodtext.setText(foodname);
        
        
        EditText foodnametxt = (EditText) findViewById(R.id.foodnametxt);
        EditText grouptxt = (EditText) findViewById(R.id.grouptxt);
        
        foodnametxt.setText(foodname);
        grouptxt.setText(catename);
        
        
        
        db = new DBContactHelper(SubFoodFormActivity.this);
        savebtn = (Button) findViewById(R.id.savebtn);
        cancelbtn = (Button) findViewById(R.id.cancelbtn);
        savebtn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    FoodDTO food = new FoodDTO();
                    food.setCateno(cateno);
                    food.setFoodname(foodname);
                    food.setBuydate(mDateDisplay.getText().toString());
                    food.setSelfdate(mDateDisplay2.getText().toString());
                    food.setPlace(place);
                    
                    db.addUserInfo(food);
                    
                    
                    Intent intent = new Intent(getApplicationContext(),TabtestActivity.class);
                    intent.putExtra("place",place);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                        
                }
        });
        cancelbtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),TabtestActivity.class);
                intent.putExtra("place",place);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                    
            }
    });
                
                
                
        mDateDisplay = (EditText) findViewById(R.id.timetext1);
        mDateDisplay2 = (EditText) findViewById(R.id.timetext2);
        mDateDisplay.setFocusableInTouchMode(false);
        mDateDisplay2.setFocusableInTouchMode(false);
        mDateDisplay.setOnClickListener(new OnClickListener() {

                @SuppressWarnings("deprecation")
                        public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
                   }
                  });
        mDateDisplay2.setOnClickListener(new OnClickListener() {
                  
                   @SuppressWarnings("deprecation")
                public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID2);
                   }
                  });
        
        // (3) 현재 날짜 인식
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth= c.get(Calendar.MONTH);
        mDay  = c.get(Calendar.DAY_OF_MONTH);
        
        c.add(Calendar.DATE, +selflife);
        mYear2 = c.get(Calendar.YEAR);
        mMonth2= c.get(Calendar.MONTH);
        mDay2  = c.get(Calendar.DAY_OF_MONTH);
        
        // (4) 인식된 날짜를 출력
        updateDisplay();
        
    }
    
                 protected Dialog onCreateDialog(int id) {
                  switch(id){
                  case DATE_DIALOG_ID : return new DatePickerDialog(this,mDateSetListener,mYear,mMonth,mDay);
                  case DATE_DIALOG_ID2 : return new DatePickerDialog(this,mDateSetListener2,mYear2,mMonth2,mDay2);
                  }
                     
                  return null;
                 }
    
            private DatePickerDialog.OnDateSetListener mDateSetListener = 
                        new DatePickerDialog.OnDateSetListener() {
          
                  public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                   mYear = year;
                   mMonth=monthOfYear;
                   mDay=dayOfMonth;
                   // 사용자가 지정한 날짜를 출력
                   updateDisplay();
                   
                  }
         };
 
         private DatePickerDialog.OnDateSetListener mDateSetListener2 = new DatePickerDialog.OnDateSetListener() {
                  
                  public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                   mYear2 = year;
                   mMonth2=monthOfYear;
                   mDay2=dayOfMonth;
                   updateDisplay();
                   
                  }
         };

 // 설정된 날짜를 TextView에 출력
    private void updateDisplay() {
     mDateDisplay.setText(
       new StringBuilder()
       .append(mYear).append("-")
       .append(mMonth+1).append("-")
       .append(mDay).append(" ")
       );
     
     mDateDisplay2.setText(
               new StringBuilder()
               //월은 시스템에서 0~11로 인식하기 때문에 1을 더해줌
               .append(mYear2).append("-")
               .append(mMonth2+1).append("-")
               .append(mDay2).append(" ")
               );
  
 }

}
